<?php

declare (strict_types = 1);

namespace app\service\system;

use app\model\system\LoggerModel;
use app\model\system\LoggerLoginModel;
use app\model\system\MenuModel;
use app\model\system\LoggerOperModel;

class LoggerService
{
    /**
     * 登录日志列表
     * @param array $param
     */
    public function loginList(array $param): array
    {
        $map = [];
        //账户
        if($username = $param['username']??'') {
            $map[] = ['username', 'like', '%'.trim($username).'%'];
        }
        //地址ip
        if($ip = $param['ip']??'') {
            $map[] = ['ip', 'like', '%'.trim($ip).'%'];
        }
        //操作系统
        if($os = $param['os']??'') {
            $map[] = ['os', 'like', '%'.trim($os).'%'];
        }
        //时间
        if($time = $param['time_stamp']??'') {
            $timeArr = explode(',', $time);
            $map[] = ['time_stamp', '>=', $timeArr[0] / 1000];
            $map[] = ['time_stamp', '<=', $timeArr[1] / 1000];
        }
        //状态
        $status = $param['status']??'';
        if(is_numeric($status)) {
            $map[] = ['status', '=', $status];
        }
        $list = LoggerLoginModel::where($map)
            ->order('id desc')
            ->pages($param)
            ->select();
        return list_fmt($list, LoggerLoginModel::where($map)->count());
    }
    /**
     * 操作日志列表
     * @param array $param
     */
    public function operList(array $param): array
    {
        $map = [];
        //账户
        if($username = $param['username']??'') {
            $map[] = ['username', 'like', '%'.trim($username).'%'];
        }
        //地址ip
        if($ip = $param['ip']??'') {
            $map[] = ['ip', 'like', '%'.trim($ip).'%'];
        }
        $list = LoggerOperModel::where($map)
            ->order('id desc')
            ->pages($param)
            ->select();
        return list_fmt($list, LoggerOperModel::where($map)->count());
    }
    /**
     * 操作日志详情
     * @param integer $id
     */
    public function operInfo(int $id)
    {
        $info = LoggerOperModel::field('id,request_data,response_code,response_data')->find($id);
        if(!$info) {
            return $info;
        }
        if($info->request_data) {
            $info->request_data = json_decode($info->request_data, true);
        }
        if($info->response_data) {
            $info->response_data = json_decode($info->response_data, true);
        }
        return $info;
    }
    /**
     * 操作日志记录
     * @param array $operLog
     * @param mixed $code 响应状态码
     * @param mixed $content 响应数据
     */
    public static function operRecord(array $record, $code, $content): void
    {
        $record['response_code'] = $code;
        $record['response_data'] = $content;
        //业务模块
        $service = MenuModel::alias('c')->field('c.title ct,p.title pt')
            ->where('c.id', $record['service_name'])
            ->leftJoin('o_system_menu p', 'p.id = c.parent_id')
            ->find();
        $record['service_name'] = '【' . $service->pt . '】' . $service->ct;
        $record['created_at'] = now_date();
        $record['time_stamp'] = time();
        LoggerOperModel::insert($record);
    }
    /**
     * 系统日志列表
     * @param array $param
     */
    public function systemList(array $param): array
    {
        $map = [];
        //日志模块
        if($module = $param['module']??'') {
            $map[] = ['module', '=', $module];
        }
        //日志等级
        if($level = $param['level']??'') {
            $map[] = ['level', '=', $level];
        }
        //日志标签
        if($tag = $param['tag']??'') {
            $map[] = ['tag', 'like', '%'.trim($tag).'%'];
        }
        //端口
        if($port = $param['port'] ?? '') {
            $map[] = ['port', '=', $port];
        }
        
        $list = LoggerModel::where($map)
            ->order('id desc')
            ->pages($param)
            ->select();
        return list_fmt($list, LoggerModel::where($map)->count());
    }
    
}